package wxk.interview.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/7.
 * @since 2.2
 */
@WebServlet(name = "MySimpleWithAnnotationServlet", urlPatterns = "/mySimpleWithAnnotationServlet")
public class MySimpleWithAnnotationServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Cookie cookie = new Cookie("sessionId", "12345678");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(-20);
        resp.addCookie(cookie);
        Map<String, String> data = getData();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>欢迎您</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>欢迎您" + data.get("userName") + "在:" + data.get("registerTime") + "注册本网站!--" + req.getRemoteAddr() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }

    }

    private Map<String, String> getData() {
        Map<String, String> data = new HashMap<String, String>();
        data.put("registerTime", new Date().toString());
        data.put("userName", "admin");
        return data;
    }
}
